<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="仓检员姓名" class="myform-item">
          <el-input
            v-model="form.username"
            placeholder="请输入仓检员姓名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="仓检日期" class="myform-item">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.qc_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
        <el-form-item style="margin-top: 50px; margin-left: 10px">
          <download-excel
            class="export-excel-wrapper"
            :data="json_data"
            :fields="json_fields"
            :before-generate="startDownload"
            title="仓检质检员"
            name="仓检质检员.xls"
          >
            <el-button type="">导出</el-button>
          </download-excel>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="仓检员姓名" align="center" prop="username">
      </el-table-column>
      <el-table-column label="合格数" align="center" prop="pass">
      </el-table-column>
      <el-table-column label="不合格数" align="center" prop="nopass">
      </el-table-column>
      <el-table-column label="总数" align="center" prop="passtotal">
      </el-table-column>
      <el-table-column label="合格率" align="center" prop="passrate">
        <template slot-scope="{ row }">
          {{ (row.passrate * 100).toFixed(2) }}%
        </template>
      </el-table-column>
      <el-table-column label="仓检记录" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" @click="onLook(row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postApiCodeGetQtuserlist"
    />
  </div>
</template>

<script>
import { postApiCodeGetQtuserlist } from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      date: "",
      form: {
        username: "",
        qc_time: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      json_data: [],
      json_fields: {
        仓检员姓名: "username",
        合格数: "pass",
        不合格数: "nopass",
        总数: "passtotal",
        合格率: "passrate",
      },
    };
  },
  activated() {
    let form = this.form;
    this.postApiCodeGetQtuserlist(form);
  },
  methods: {
    // 查看
    onLook(id) {
      this.$router.push({ name: "CangRecord", query: { id } });
    },
    // 编辑
    onEdit(e) {
      console.log("编辑", e);
      this.detail = e;
      this.dialogVisible = true;
    },
    // qc列表
    async postApiCodeGetQtuserlist(data) {
      this.loading = true;
      this.form = data;
      let form = JSON.parse(JSON.stringify(this.form));
      if (form.qc_time) {
        form.qc_time = `${form.qc_time[0]}/${form.qc_time[1]}`;
      }
      postApiCodeGetQtuserlist(form)
        .then((res) => {
          this.loading = false;
          console.log("qc列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    async startDownload() {
      this.loading = true
      let { form } = this;
      let json_form = JSON.parse(JSON.stringify(form));
      if (json_form.qc_time) {
        json_form.qc_time = `${form.qc_time[0]}/${form.qc_time[1]}`;
      }
      json_form.size = 1000;
      let res = await postApiCodeGetQtuserlist(json_form);
      this.json_data = res.data;
      this.loading = false
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postApiCodeGetQtuserlist(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
